import type { UploadProgressEvent } from "element-plus";

import { PORT1 } from "@/api/config/servicePort";

import http from "@/api";

interface OptionType {
  key: number;
  value: string;
}

export interface TreeOptionType extends OptionType {
  sublevel?: TreeOptionType[];
}

// 获取员工列表统计数据
export const getStatistics = (params: { org_company?: number; org_department?: number }, queueId?: string) =>
  http.get<{ [k: string]: any[] }>(`${PORT1}/personnel/personnel/statistics`, { ...params }, { queueId, loading: false });

// 获取新工号
export const getNewJobNo = (queueId?: string) =>
  http.post<{ job_no: string }>(`${PORT1}/personnel/personnel/get-new-job-no`, {}, { queueId, loading: false });

// 获取员工卡片信息
export const getPersonnelCard = (user_id: string | number, queueId?: string) =>
  http.post<Record<string, any>>(`${PORT1}/personnel/personnel/card`, { user_id }, { queueId, loading: false });

// 获取员工个人信息
export const getPersonnelInfo = (user_id: string, queueId?: string) =>
  http.post<Record<string, any>>(`${PORT1}/personnel/personnel-info/user-info`, { user_id }, { queueId, loading: false });

// 获取员工在职信息
export const getPersonnelJob = (user_id: string, queueId?: string) =>
  http.post<Record<string, any>>(`${PORT1}/personnel/personnel-info/job-info`, { user_id }, { queueId, loading: false });

// 获取员工合同信息
export const getPersonnelContract = (user_id: string, queueId?: string) =>
  http.get<Record<string, any>[]>(
    `${PORT1}/personnel/contract-manage/get-contracts-by-user-id`,
    { user_id },
    { queueId, loading: false }
  );

// 编辑员工信息
export const savePersonnelInfo = (data: Record<string, any>) =>
  http.post(`${PORT1}/personnel/personnel-info/edit-info`, { ...data }, { loading: false });

// 解除员工信息
export const securePersonnelInfo = (user_contract_id: string) =>
  http.get(`${PORT1}/personnel/contract-manage/terminate-contract`, { user_contract_id }, { loading: false });

// 删除员工信息
export const deletePersonnelInfo = (user_contract_id: string) =>
  http.get(`${PORT1}/personnel/contract-manage/delete`, { user_contract_id }, { loading: false });

// 添加或编辑员工合同信息
export const savePersonnelContract = (data: Record<string, any>) =>
  http.post<string | number>(`${PORT1}/personnel/contract-manage/save`, { ...data }, { loading: false });

// 添加新员工
export const addPersonnel = (data: any) => http.post(`${PORT1}/personnel/personnel/add`, { ...data }, { loading: false });

// 新员工查重校验
export const checkDuplicate = (data: { field: string; value: string | number; user_id?: string }) =>
  http.post(`${PORT1}/personnel/personnel/verify-params`, { ...data }, { loading: false });

// 获取员工字典项
export const getDictOptions = (data: { key: string; parent_id?: string | number }, queueId?: string) =>
  http.post<Array<OptionType>>(`${PORT1}/personnel/personnel/search-item`, { ...data }, { queueId, loading: false });

export const getOrgTreeOptions = (queueId?: string) =>
  http.post<Array<TreeOptionType>>(`${PORT1}/personnel/personnel/orgs-tree`, {}, { queueId, loading: false });

// 获取合同字典项
// export const getDictOptionsByContract = (data: { key: string; parent_id?: string | number }, queueId?: string) =>
//   http.post<Array<{ key: number; value: string }>>(
//     `${PORT1}/personnel/personnel/search-item`,
//     { ...data },
//     { queueId, loading: false }
//   );

export interface UploadType {
  user_file_id: string;
  user_id: string;
  user_file_name: string;
  user_file_path: string;
  user_file_type: string;
  extension_name: string;
}

// 获取附件
export const getFiles = (user_id: string) =>
  http.post<Record<any, Array<UploadType>>>(`${PORT1}/personnel/personnel-info/get-user-files`, { user_id }, { loading: false });

// 上传附件
export const uploadFiles = (
  user_id: string,
  user_file_type: number,
  data: FormData,
  onProgress?: (progressEvt: UploadProgressEvent) => void
) =>
  http.post(`${PORT1}/personnel/personnel-info/upload-user-files?user_id=${user_id}&user_file_type=${user_file_type}`, data, {
    headers: { "Content-Type": "multipart/form-data" },
    onUploadProgress: (evt: any) => {
      const progressEvt = evt as UploadProgressEvent;
      progressEvt.percent = evt.total > 0 ? (evt.loaded / evt.total) * 100 : 0;
      onProgress?.(progressEvt);
    },
    loading: false,
    cancel: false
  });

// 删除附件
export const deleteFiles = (user_id: string, user_file_id: string) =>
  http.post(`${PORT1}/personnel/personnel-info/del-user-files`, { user_id, user_file_id }, { loading: false });
